package com.zztl.infiniteBand.adapter

import android.content.Context
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.BaseAdapter
import android.widget.ImageView
import android.widget.TextView
import com.zztl.infiniteBand.R
import com.zztl.infiniteBand.model.WatchBean
import java.util.*

/**
 * @desc
 * @auth ${user}
 * @time 2018/9/20  11:19
 */
class MyDeviceAdapter(data: List<WatchBean>, private val mContext: Context) : BaseAdapter() {

    private var data = ArrayList<WatchBean>()

    init {
        this.data = data as ArrayList<WatchBean>
    }

    override fun getCount(): Int {
        return data.size
    }

    override fun getItem(position: Int): Any {
        return data[position]
    }

    override fun getItemId(position: Int): Long {
        return position.toLong()
    }

    override fun getView(position: Int, convertView: View?, parent: ViewGroup): View {
        val view: View
        val viewHolder: ViewHolder
        if (convertView == null) {
            view = LayoutInflater.from(mContext).inflate(R.layout.item_my_device, parent, false)
            viewHolder = ViewHolder()
            viewHolder.imageView = view.findViewById(R.id.watch_image)
            viewHolder.textView = view.findViewById(R.id.tv_watch_name)
            view.tag = viewHolder
        } else {
            view = convertView
            viewHolder = view.tag as ViewHolder
        }

        viewHolder.textView!!.text = data[position].name
        viewHolder.imageView!!.setImageResource(if (data[position].type == 1) R.mipmap.watch02 else R.mipmap.watch01)
        return view
    }

    private inner class ViewHolder {
         var imageView: ImageView? = null
         var textView: TextView? = null
    }
}
